<template>
  <el-dialog v-model="editFormVisible" title="创建任务" width="60%">
    <el-form :model="data" label-width="auto">
      <el-form-item label="任务类型">
        <el-select v-model="data.TaskType">
          <el-option v-for="o in taskTypeOptions" :key="o.value" :label="o.label" :value="o.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="起始点位">
        <el-select v-model="data.FromStation" filterable remote :remote-method="getFromStationOptions">
          <el-option v-for="o in fromStationOptions" :key="o.value" :label="o.label" :value="o.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="目标点位">
        <el-select v-model="data.ToStation" filterable remote :remote-method="getToStationOptions">
          <el-option v-for="o in toStationOptions" :key="o.value" :label="o.label" :value="o.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务条码">
        <el-input v-model="data.Barcode" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script>
import zFormDialog from '@/views/_common/formDialog.vue'
import _ from 'lodash'
export default {
  name: 'create-task-dialog',
  components: {
    zFormDialog,
  },
  props: {
    modelValue: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    editFormVisible: {
      get() {
        return this.modelValue
      },
      set(v) {
        this.$emit('update:modelValue', v)
      },
    },
  },
  data() {
    return {
      initData: {},
      data: {},
      taskTypeOptions: [
        { label: '出库', value: 1 },
        { label: '入库', value: 2 },
        { label: '移库', value: 3 },
        { label: 'agv入库', value: 4 },
      ],
      fromStationOptions: [],
      toStationOptions: [],
      submitLoading: false,
    }
  },
  created() {
  },
  methods: {
    getFromStationOptions(locat) {
      if (!locat) return
      this.http.get(this.$api.wcsUrl + 'wcs/location/GetAllLocation?locat=' + locat).then((r) => {
        let options = r.map((x) => {
          return {
            label: x,
            value: x,
          }
        })
        this.fromStationOptions = options
      })
    },
    getToStationOptions(locat) {
      if (!locat) return
      this.http.get(this.$api.wcsUrl + 'wcs/location/GetAllLocation?locat=' + locat).then((r) => {
        let options = r.map((x) => {
          return {
            label: x,
            value: x,
          }
        })
        this.toStationOptions = options
      })
    },

    formatBeforeSave(data) {
      let tmp = _.cloneDeep(data)
      let items = {
        items: [{ ...tmp, Seq: 1 }],
      }
      return { ...tmp, ...items }
    },
    submit() {
      let data = this.formatBeforeSave(this.data)
      let url = this.$api.wcsUrl + 'wcs/task/addtask'
      this.submitLoading = true
      this.http.post(url, data)
        .then((r) => {
          this.$message.success('操作成功')
          this.editFormVisible = false
          this.data = {}
          this.$emit('succesCallBack')
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped></style>
